package io;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Work {
    public static void main(String[] args) throws IOException {
        //单字节写
        /*
        File file = new File("fos.dat");
        FileOutputStream fos = new FileOutputStream(file,true);
        fos.write(1);
        fos.write(2);
        System.out.println("写出完毕!");
        fos.close();
         */
        /*
        File file = new File("fos.dat");
        FileOutputStream fos = new FileOutputStream(file,true);
        fos.write(1);
        fos.write(2);
        System.out.println("写出完毕!");
        fos.close();

         */


        //单字节读
        /*
        FileInputStream fis = new FileInputStream("fos.dat");
        int d = fis.read();
        System.out.println(d);
        d = fis.read();
        System.out.println(d);
        d = fis.read();
        System.out.println(d);
        fis.close();
         */
        /*
        FileInputStream fis = new FileInputStream("fos.dat");
        int d = fis.read();
        System.out.println(d);
        d = fis.read();
        System.out.println(d);
        d = fis.read();
        System.out.println(d);
        fis.close();
         */

        //单字节文件复制
        /*
        FileInputStream fis = new FileInputStream("xjp.jpg");
        FileOutputStream fos = new FileOutputStream("xjp_cp.jpg");
        long start = System.currentTimeMillis();
        int d;
        while ((d = fis.read()) != -1) {
            fos.write(d);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕!耗时:"+(end - start)+"ms");
        fis.close();
        fos.close();
         */
        /*
        FileInputStream fis = new FileInputStream("xjp.jpg");
        FileOutputStream fos = new FileOutputStream("xjp_cp.jpg");
        long start = System.currentTimeMillis();
        int d;
        while ((d = fis.read()) != -1) {
            fos.write(d);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕!耗时:"+(end - start)+"ms");
        fis.close();
        fos.close();
         */

        //块读写的文件复制操作
        /*
        FileInputStream fis = new FileInputStream("xjp.jpg");
        FileOutputStream fos = new FileOutputStream("xjp_cp2.jpg");
        long start = System.currentTimeMillis();
        byte[] data = new byte[1024*10];
        int len;
        while ((len = fis.read(data))!=-1){
            fos.write(data,0,len);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕!耗时:"+(end-start)+"ms");
        fis.close();
        fos.close();
         */
        /*
        FileInputStream fis = new FileInputStream("xjp.jpg");
        FileOutputStream fos = new FileOutputStream("xjp_cp2.jpg");
        long start = System.currentTimeMillis();
        byte[] data = new byte[1024*10];
        int len;
        while ((len = fis.read(data))!=-1){
            fos.write(data,0,len);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕!耗时:"+(end-start)+"ms");
        fis.close();
        fos.close();
         */

        //写文本数据
        /*
        FileOutputStream fos = new FileOutputStream("fos.txt",true);
        String line = "心在跳是爱情如烈火，你在笑疯狂的人是我";
        byte[] data = line.getBytes(StandardCharsets.UTF_8);
        fos.write(data);
        fos.write(",爱如火会温暖了心窝".getBytes(StandardCharsets.UTF_8));
        System.out.println("写出完毕!");
        fos.close();
         */
         /*
        FileOutputStream fos = new FileOutputStream("fos.txt",true);
        String line = "心在跳是爱情如烈火，你在笑疯狂的人是我";
        byte[] data = line.getBytes(StandardCharsets.UTF_8);
        fos.write(data);
        fos.write(",爱如火会温暖了心窝".getBytes(StandardCharsets.UTF_8));
        System.out.println("写出完毕!");
        fos.close();
          */

        //读取文本数据
        /*
        File file = new File("fos.txt");
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        fis.read(data);
        String line = new String(data,StandardCharsets.UTF_8);
        System.out.println(line);
        fis.close();
         */
        /*
        File file = new File("fos.txt");
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        fis.read(data);
        String line = new String(data,StandardCharsets.UTF_8);
        System.out.println(line);
        fis.close();

         */

    }
}